import Utils from "../common/Util";
import PropManager from "../game/PropManager";
import GameManager from "../common/GameManager";

// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class PropLayer extends cc.Component {

    @property(cc.Node)
    button: cc.Node = null;

    @property(cc.Node)
    finger: cc.Node = null;

    @property(cc.Node)
    hammer: cc.Node = null;

    @property([cc.Node])
    xCount: cc.Node[] = [];

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    private type: number;
    private num: number;
    private propNode: cc.Node;
    private opening: boolean;

    start() {
    }

    public init(type: number, num: number) {
        this.type = type;
        this.num = num;
        if (type == PropManager.TYPE_FINGER) {
            this.finger.active = true;
            this.hammer.active = false;
            this.propNode = this.finger;
        } else if (type == PropManager.TYPE_HAMMER) {
            this.finger.active = false;
            this.hammer.active = true;
            this.propNode = this.hammer;
        }

        for (var i = 0; i < this.xCount.length; i++) {
            if (this.xCount[i]) {
                this.xCount[i].active = (i + 1 == num);
            }
        }
    }

    public async open() {
        if (this.opening) return;
        this.opening = true;
        let self = this;
        GameManager.getInstance().eventCenter.DispatchEvent("getProp", {
            type: this.type,
            node: this.propNode,
            pos: self.node.convertToWorldSpaceAR(this.propNode.position),
            cb: () => {
                PropManager.getInstance().changeProp(this.type, this.num);
            },
            fade: () => {
                self.node.runAction(cc.sequence(cc.fadeOut(3.3), cc.callFunc(() => {
                    self.node.destroy();
                })));
            }
        })
    }

    // update (dt) {}
}
